﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreClientModuleEx;
using UnityCore;

namespace MBBStoreClient
{
    public partial class frmLogViewer : Form
    {
        StockManager sm = new StockManager();
        Threader t = new Threader();
        int _page=0;
        StockManagerType _lastSearch = new StockManagerType();
        public frmLogViewer()
        {
            InitializeComponent();

            btnClose.Click += new EventHandler(btnClose_Click);
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);
            btnSearch.Click += new EventHandler(btnSearch_Click);

            sm.ProcessCompleted += new UnityCore.CoreHandler(sm_ProcessCompleted);
            sm.ProcessError += new UnityCore.CoreHandler(sm_ProcessError);

            btnPrev.Click += new EventHandler(btnPrev_Click);
            btnNext.Click += new EventHandler(btnNext_Click);
            chkDateSpec.Click += new EventHandler(chkDateSpec_Click);
        }

        void chkDateSpec_Click(object sender, EventArgs e)
        {
            if (chkDateSpec.Checked)
            {
                txtDate.Enabled = true;
            }
            else
            {
                txtDate.Enabled = false;
            }
        }

        void btnNext_Click(object sender, EventArgs e)
        {
            _page++;
            _lastSearch.mPage = _page;
            sm.GetLog(_lastSearch);
        }
        void btnPrev_Click(object sender, EventArgs e)
        {
            _page--;
            _lastSearch.mPage = _page;
            sm.GetLog(_lastSearch);
        }
        void btnSearch_Click(object sender, EventArgs e)
        {
            _page = 1;
            string ddate = OdbcAccess.ToOdbcDate(txtDate.Value);
            if(!chkDateSpec.Checked) ddate = "";
            _lastSearch = new StockManagerType()
            {
                fUser = txtUser.Text,
                fBarcode = txtBarcode.Text,
                fDate = ddate,
                mCnt = 50,
                mPage = _page
            };
            sm.GetLog(_lastSearch);
        }




        void ControlState(bool state)
        {
            grpFilter.Enabled = state;
            grdLog.Enabled = state;
            if (state)
            {
                pnlMsg.Visible = false;
            }
            else
            {
                pnlMsg.Visible = true;
            }
        }
        void sm_ProcessError(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                MessageBox.Show(pcode, msg);
            });
        }
        void sm_ProcessCompleted(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                switch (pcode)
                {
                    case "GetLog":
                        grdLog.DataSource = sm.LogTable;
                        grdLog.Columns[0].HeaderText = "User";
                        grdLog.Columns[1].HeaderText = "Barcode";
                        grdLog.Columns[2].HeaderText = "Amount";
                        grdLog.Columns[3].HeaderText = "Unit";
                        grdLog.Columns[4].HeaderText = "Date/Time";
                        grdLog.Columns[0].Width = (grdLog.Width * 20) / 100;
                        grdLog.Columns[1].Width = (grdLog.Width * 30) / 100;
                        grdLog.Columns[2].Width = (grdLog.Width * 15) / 100;
                        grdLog.Columns[3].Width = (grdLog.Width * 10) / 100;
                        grdLog.Columns[4].Width = (grdLog.Width * 20) / 100;

                        grdLog.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        grdLog.Columns[2].DefaultCellStyle.Format = "#,#0.00";

                        
                        lblPageNum.Text = _page.ToString();

                        if (sm.LogTable.Rows.Count == _lastSearch.mCnt)
                        {
                            if (_page != 1)
                            {
                                btnNext.Enabled = true;
                                btnPrev.Enabled = true;
                            }
                            else
                            {
                                btnNext.Enabled = true;
                                btnPrev.Enabled = false;
                            }
                        }
                        else
                        {
                            if (_page != 1)
                            {
                                btnNext.Enabled = false;
                                btnPrev.Enabled = true;
                            }
                            else
                            {
                                btnNext.Enabled = false;
                                btnPrev.Enabled = false;
                            }
                        }
                        break;
                }
            });
        }


        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }
        void btnClose_Click(object sender, EventArgs e)
        {
            pnlForm.Dispose();
            this.Dispose();
        }
        public Panel PanelForm
        {
            get { return pnlForm; }
        }
        public void Initialize(string Title)
        {
            this.Select();
            lblText.Text = Title;
        }
    }
}
